package com.turtle3d.user.grammars;

import java.util.ArrayList;

import com.turtle3d.formallanguage.grammar.Grammar;
import com.turtle3d.formallanguage.parser.SimpleParser;
import com.turtle3d.formallanguage.symbol.Method;
import com.turtle3d.formallanguage.symbol.SymbolFactory;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.util.Randomizer;

public class Branch extends Grammar
{

	@Override
	public void define()
	{
		
		SymbolFactory.getSymbolFactory().registerSymbolClass("F", "F(grow)", new Method(){
			public boolean calculate(ArrayList<Symbol> symbolSequence, int symbolIndex, Symbol calculatedSymbol)
			{
				Float r = calculatedSymbol.getParameter("radius");
				Float r_mul = calculatedSymbol.getParameter("radiusMultiplier");
				calculatedSymbol.setParameter("radius", r*r_mul);
				
				Float d = calculatedSymbol.getParameter("distance");
				Float d_mul = calculatedSymbol.getParameter("distanceMultiplier");
				calculatedSymbol.setParameter("distance", d*d_mul+(calculatedSymbol.getParameter("distanceVariation")*d*Randomizer.getRandomizer().nextVariation()));
				
				return true;
				
			}
		});	
		
		setAxiom(SymbolFactory.getSymbolFactory().createSymbolofClass("X"));
		addProduction(SimpleParser.parseProduction("X->F-[[RX]+RX]+RF[+FRX]-RX>RX>RX"));
		addProduction(SimpleParser.parseProduction("F->F(grow)"));						
		
	}
	
	public void setRequiredParameters(){
		setRequiredParameter(Symbol.TURN_ANGLE, 25f);
		setRequiredParameter(Symbol.ROLL_ANGLE, 90f);
		setRequiredParameter(Symbol.PITCH_ANGLE, 90f);
		setRequiredParameter("radiusMultiplier", 1.7f);		
		setRequiredParameter("distanceMultiplier", 1.5f);							
		setRequiredParameter("distanceVariation", 0.2f);
	}

}
